<?php

namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class ShopPointsSendLog extends Model
{
    use HasFactory;
    protected $table = "shop_points_send_list";
    protected $guarded = [];
    public static function send($data){
        $room_id=$data['room_id']??null;
        $room_group_id=$data['room_group_id']??null;
        $shop_id=$data['shop_id']??null;
        $host_id=$data['host_id']??null;
        $room_name=$data['room_id']??null;
        $shop_name=$data['shop_id']??null;
        $host_name=$data['host_id']??null;
        $host_account=$data['host_account']??null;
        $points=$data['points']??null;
        $amount=$data['amount']??null;
        $point=$data['point']??null;
        $time_long=$data['time_long']??null;
        $countdown_timer=$data['countdown_timer']??null;
        $begin_time=$data['begin_time']??null;
        $end_time=$data['end_time']??null;
        if(empty($shop_id)||empty($host_id)||empty($points)||empty($amount)){
            return null;
        }
        $shop=Redis::get(Shop::REDIS_PREFIX.$shop_id);
        $host=Redis::get(ShopHost::REDIS_PREFIX.$host_id);
        if(empty($shop)){
            return null;
        }
        if(empty($host)){
            return null;
        }
        return self::create([
            'room_id'=>$room_id,
            'room_group_id'=>$room_group_id,
            'shop_id'=>$shop_id,
            'host_id'=>$host_id,
            'room_name'=>$room_name,
            'shop_name'=>$shop_name,
            'host_name'=>$host_name,
            'host_account'=>$host_account,
            'points'=>$points,
            'amount'=>$amount,
            'point'=>$point,
            'time_long'=>$time_long,
            'countdown_timer'=>$countdown_timer,
            'begin_time'=>$begin_time,
            'end_time'=>$end_time
        ]);
    }
    public static function copy2_ShopSendLog(){

    }
    public function receivers(){
        return $this->hasMany(ShopPointsReceiveLog::class,'activity_id','id');
    }
    protected function serializeDate(DateTimeInterface $date){
        return $date->format('Y-m-d H:i:s');
    }
    public static function lastSendLog($data){
        $room_id=$data['room_id']??null;
        $shop_id=$data['shop_id']??null;
        $host_id=$data['host_id']??null;
        if(empty($shop_id)||empty($host_id)||empty($room_id)){
            return null;
        }
        $shop=Redis::GET(Shop::REDIS_PREFIX.$shop_id);
        $host=Redis::GET(ShopHost::REDIS_PREFIX.$host_id);
        if(empty($shop)){
            return null;
        }
        if(empty($host)){
            return null;
        }
        return self::where([
            'room_id'=>$room_id,
            'shop_id'=>$shop_id,
            'host_id'=>$host_id,
        ])->orderBy('id','desc')->select('amount','points','point')->first();
    }
}
